package com.quan.mall.service.impl;

import com.quan.commons.core.biz.support.MyBaseServiceImpl;
import com.quan.commons.core.utils.PageUtils;
import com.quan.mall.entity.PmsAttr;
import com.quan.mall.entity.PmsCategory;
import com.quan.mall.service.PmsAttrService;
import com.quan.mall.service.PmsCategoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.quan.mall.commons.vo.PmsAttrGroupVo;
import com.quan.mall.entity.PmsAttrGroup;
import com.quan.mall.mapper.PmsAttrGroupMapper;
import com.quan.mall.service.PmsAttrGroupService;


@Slf4j
@Service
public class PmsAttrGroupServiceImpl extends MyBaseServiceImpl<PmsAttrGroupMapper, PmsAttrGroup> implements PmsAttrGroupService {

    @Autowired
	private PmsAttrGroupMapper mapper;

    @Autowired
    private PmsCategoryService pmsCategoryService;

    @Autowired
    private PmsAttrService pmsAttrService;



    public PmsAttrGroupServiceImpl() {
    }

    @Override
    public List<PmsAttrGroupVo> queryList(PageUtils pageUtils, PmsAttrGroupVo vo) {
        QueryWrapper<PmsAttrGroup> queryWrapper = new QueryWrapper<PmsAttrGroup>(vo);

        List<PmsAttrGroup> list = super.list(pageUtils, queryWrapper);
        List<PmsAttrGroupVo> collect = list.stream().map(item -> {
            PmsAttrGroupVo bean = new PmsAttrGroupVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public PageUtils queryPage(PageUtils pageUtils, PmsAttrGroupVo vo) {
        QueryWrapper<PmsAttrGroup> queryWrapper = new QueryWrapper<PmsAttrGroup>(vo);

        if(StringUtils.isNotBlank(vo.getKeyword())) {
            queryWrapper.and(w -> {
                w.like("username", vo.getKeyword());
            });
        }

        final IPage<PmsAttrGroup> page = super.selectPage(pageUtils, queryWrapper);

        PageUtils ps = new PageUtils(page);

        final List<PmsAttrGroup> records = page.getRecords();

        final List<PmsAttrGroup> collect = records.stream().map((item) -> {
            PmsAttrGroupVo bean = new PmsAttrGroupVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.toList());

        ps.setRecords(collect);
        return ps;
    }

    @Override
    public PageUtils queryAttrGroupPage(PageUtils pageUtils, PmsAttrGroup entity) {
        QueryWrapper<PmsAttrGroup> queryWrapper = new QueryWrapper<PmsAttrGroup>(entity);

        // 模糊查询
        if(StringUtils.isNotEmpty(entity.getKeyword())) {
            queryWrapper.and(w -> {
                w.like("attr_group_name", entity.getKeyword()).or().like("descript", entity.getKeyword());
            });
        }

        queryWrapper.eq(entity.getCateId() != null, "cate_id", entity.getCateId());

        final IPage<PmsAttrGroup> page = super.selectPage(pageUtils, queryWrapper);

        PageUtils ps = new PageUtils(page);

        final List<PmsAttrGroup> records = page.getRecords();

        final List<PmsAttrGroup> collect = records.stream().map((item) -> {
            PmsAttrGroupVo attrGroupVo = new PmsAttrGroupVo();
            BeanUtils.copyProperties(item, attrGroupVo);

            PmsCategory category = pmsCategoryService.getById(item.getCateId());
            if (category != null) {
                attrGroupVo.setCategoryName(category.getName());
            }

            return attrGroupVo;
        }).collect(Collectors.toList());

        ps.setRecords(collect);

        return ps;
    }

    @Override
    public List<PmsAttrGroupVo> getAttrGroupWithAttrsByCatelogId(Long cateId) {
        //1、查询分组信息
        List<PmsAttrGroup> attrGroupEntities = this.list(new QueryWrapper<PmsAttrGroup>().eq("cate_id", cateId));

        //2、查询所有属性
        List<PmsAttrGroupVo> collect = attrGroupEntities.stream().map(item -> {
            PmsAttrGroupVo attrGroupVo = new PmsAttrGroupVo();
            BeanUtils.copyProperties(item,attrGroupVo);

            List<PmsAttr> attrs = this.pmsAttrService.getGroupAttrs(attrGroupVo.getId());
            attrGroupVo.setAttrs(attrs);

            return attrGroupVo;
        }).collect(Collectors.toList());

        return collect;
    }
}